﻿namespace Creek.UI.EFML.Base
{
    using System.IO;
    using System.Xml;

    public class EFMLDocument
    {
        #region Fields

        private readonly XmlDocument doc = new XmlDocument();

        #endregion

        #region Public Properties

        public XmlNode Body
        {
            get
            {
                return this.doc.GetElementsByTagName("body")[0];
            }
        }

        public XmlNode Head
        {
            get
            {
                return this.doc.GetElementsByTagName("head")[0];
            }
        }

        public XmlNodeList Meta
        {
            get
            {
                return this.doc.GetElementsByTagName("meta");
            }
        }

        public XmlNodeList Scripts
        {
            get
            {
                return this.doc.GetElementsByTagName("script");
            }
        }

        public XmlNodeList Stylesheets
        {
            get
            {
                return this.doc.GetElementsByTagName("style");
            }
        }

        #endregion

        #region Public Methods and Operators

        public static EFMLDocument Load(string efml)
        {
            var r = new EFMLDocument();
            if (efml.StartsWith("???"))
            {
                efml = efml.Remove(0, 3);
            }
            r.doc.Load(new StringReader(efml));
            return r;
        }

        #endregion
    }
}